package zookeeper.code;

import Hbase.HbaseClient;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author: yehw
 * @date: 2020/7/25 19:39
 * @description: 客户端测试
 */
public class ZkClientTest {


    public static void main(String[] args) {
        new ZkClientTest().getZookeeper();
    }

    public void getZookeeper() {
        Connection connection = null;
        ZkClient zkClient = new ZkClient("linux121:2181");
        //设置⾃定义的序列化类型,否则会报错！！
        zkClient.setZkSerializer(new ZkStrSerializer());
        //判断节点是否存在，不存在创建节点并赋值
        final boolean exists = zkClient.exists("/datasouce");
        if (!exists) {
            zkClient.createEphemeral("/datasouce", "jdbc:mysql://linux123:3306/mysql?useSSL=true&serverTimezone=GMT;root;09260926;com.mysql.jdbc.Driver");
        }
        String datasouce = (String) zkClient.readData("/datasouce");
        String[] info = datasouce.split(";");
        String url = info[0];
        String username = info[1];
        String password = info[2];
        String driverClass = info[3];
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("初始化链接是：" + connection);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        //注册监听器，节点数据改变的类型，接收通知后的处理逻辑定义
        zkClient.subscribeDataChanges("/datasouce", new IZkDataListener() {
            public void handleDataChange(String path, Object data) throws
                    Exception {
                //定义接收通知之后的处理逻辑
                String datasource = (String) data;
                String[] info = datasource.split(";");
                String url = info[0];
                String username = info[1];
                String password = info[2];
                String driverClass = info[3];
                getConnenction(url,username,password,driverClass);
            }

            //数据删除--》节点删除
            public void handleDataDeleted(String path) throws Exception {
                System.out.println(path + " is deleted!!");
            }
        });
    }

    ;

    public Connection getConnenction(String url, String username, String password, String driverClass) {
        Connection connection = null;
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("链接是：" + connection);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return null;
    }
       HbaseClient client=new HbaseClient();
}
